package com.lmh.graphdisease.utils;

import com.lmh.graphdisease.entity.*;
import org.neo4j.driver.internal.value.NodeValue;

import java.util.*;

/**
 * @author lmh
 * @desc NodeValue实体属性转化到各个节点，属性对应，用于neo4j数据库数据查询返回
 * @date 2023/3/7
 */
public class MatchEntityCastUtil {


    /**
     *@author lmh
     *@date 2023/3/7
     *@desc 通过neo4j-driver中的NodeValue进行处理,转化成DiseaseEntity类型
     */
    public static DiseaseEntity MatchToDiaseaseEntity(NodeValue nodeValue){
        //[prevent, cure_way, name, cure_lasttime, cured_prob, cause, cure_department, easy_get, desc]

        String curName = nodeValue.asEntity().get("name").asString();
        String curPrevent = nodeValue.asEntity().get("prevent").asString();
        String curCause = nodeValue.asEntity().get("cause").asString();
        String curCurelasttime = nodeValue.asEntity().get("cure_lasttime").asString();
        String curCureprob = nodeValue.asEntity().get("cure_prob").asString();
        String curEasyget = nodeValue.asEntity().get("easy_get").asString();
        String curDesc = nodeValue.asEntity().get("desc").asString();
        List<String> curCureWay = new ArrayList<>();
        List<Object> curCurewayTemp = nodeValue.asEntity().get("cure_way").asList();
        for (Object o:curCurewayTemp){
            curCureWay.add(o.toString());
        }
        List<Object> curCuredepartmentTemp = nodeValue.asEntity().get("cure_department").asList();
        List<String> curCuredepartment = new ArrayList<>();
        for (Object o:curCuredepartmentTemp){
            curCuredepartment.add(o.toString());
        }
        DiseaseEntity diseaseEntity = new DiseaseEntity(nodeValue.asEntity().id(),curName,curDesc,curCause,curPrevent,curCurelasttime,curCureWay,curCureprob,curEasyget,curCuredepartment);

        return diseaseEntity;
    }

    public static CheckEntity MatchToCheckEntity(NodeValue nodeValue){
        String curName = nodeValue.asEntity().get("name").asString();
        CheckEntity checkEntity = new CheckEntity(nodeValue.asEntity().id(),curName);
        return checkEntity;
    }

    public static DrugEntity MatchToDrugEntity(NodeValue nodeValue){
        String curName = nodeValue.asEntity().get("name").asString();
        DrugEntity drugEntity = new DrugEntity(nodeValue.asEntity().id(),curName);
        return drugEntity;
    }

    public static FoodEntity MatchToFoodEntity(NodeValue nodeValue){
        String curName =  nodeValue.asEntity().get("name").asString();
        FoodEntity foodEntity = new FoodEntity(nodeValue.asEntity().id(),curName);
        return foodEntity;
    }

    public static ProducerEntity MatchToProducerEntity(NodeValue nodeValue){
        String curName = nodeValue.asEntity().get("name").asString();
        ProducerEntity producerEntity = new ProducerEntity(nodeValue.asEntity().id(),curName);
        return producerEntity;
    }

    public static SymptomEntity MatchToSymptomEntity(NodeValue nodeValue){
        String curName = nodeValue.asEntity().get("name").asString();
        SymptomEntity symptomEntity = new SymptomEntity(nodeValue.asEntity().id(),curName);
        return symptomEntity;
    }

    public static DepartmentEntity MatchToDepartmentEntity(NodeValue nodeValue){
        String curName = nodeValue.asEntity().get("name").asString();
        DepartmentEntity departmentEntity = new DepartmentEntity(nodeValue.asEntity().id(),curName);
        return departmentEntity;
    }


    public static Map<String,Object> MatchToEchartsMap(DiseaseEntity diseaseEntity,List<CheckEntity> checkList,
                                                       List<DepartmentEntity> departmentList,List<DrugEntity> drugList,
                                                       List<SymptomEntity> symptomList,List<FoodEntity> foodList){
        if(diseaseEntity == null) return null;
        String name = diseaseEntity.getName();
        List<String> relationShips = new ArrayList<>();

        Set<String> diseaseSet = new HashSet<>();
        diseaseSet.add(name);

        Set<String> checkSet = new HashSet<>();
        for (CheckEntity cur:checkList){
            String curName = cur.getName();
            checkSet.add(curName);
            relationShips.add(name + "-" + "需要检查" + "-" + curName);
        }

        Set<String> departmentSet = new HashSet<>();
        for (DepartmentEntity cur:departmentList){
            String curName = cur.getName();
            departmentSet.add(curName);
            relationShips.add(name + "-" + "所属科室" + "-" + curName);
        }

        Set<String> drugSet = new HashSet<>();
        for (DrugEntity cur:drugList){
            String curName = cur.getName();
            drugSet.add(curName);
            relationShips.add(name + "-" + "推荐药物" + "-" + curName);
        }

        Set<String> foodSet = new HashSet<>();
        for (FoodEntity cur:foodList){
            String curName = cur.getName();
            foodSet.add(curName);
            relationShips.add(name + "-" + "推荐食物" + "-" + curName);
        }

        Set<String> symptomSet = new HashSet<>();
        for (SymptomEntity cur:symptomList){
            String curName = cur.getName();
            symptomSet.add(curName);
            relationShips.add(name + "-" + "表现症状" + "-" + curName);
        }
        Map<String,Object> result = new HashMap<>();
        result.put("disease",diseaseSet);
        result.put("department",departmentSet);
        result.put("check",checkSet);
        result.put("drug",drugSet);
        result.put("food",foodSet);
        result.put("symptom",symptomSet);
        result.put("relationships",relationShips);

        return result;
    }


}
